<template>
  <el-dialog
    :title="title"
    :visible.sync="showChild"
    :before-close="beforeClose"
    :close-on-click-modal="false"
  >
    <div class="form-container">
      <el-form :model="position" ref="form" label-width="92px" :rules="rules">
        <el-form-item prop="positionName" label="职位名称">
          <el-input v-model.trim="position.positionName" placeholder="请输入职位名称"></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model.trim="position.remark" type="textarea" :rows="5" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-form-item prop="sortIndex" label="序号">
          <el-input-number v-model="position.sortIndex" @change="handSortIndexChange" :min="1"
                           :max="99"></el-input-number>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">

      <el-button type="default" @click="close">取 消</el-button>
      <el-button type="danger" @click="resetForm('form')">重 置</el-button>
      <el-button type="success" @click="submitForm('form')" :loading="changeLoading">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import positionAPI from '@/api/system/position'

  export default {
    name: "positionCommon",
    data() {
      return {
        position:{
          positionId:'',
          positionName:'',
          remark:'',
          sortIndex:''
        },
        rules: {
          positionName: [
            {required: true, message: '请输入职位名称', trigger: 'blur'}
          ]
        },
        showChild: false,
        title: '新增职位',
        changeLoading:false,
      }
    },
    created() {
    },
    methods: {
      close(){
        this.resetForm('form')
        this.showChild = false
      },
      beforeClose(done){
        this.resetForm('form')
        done(true)
      },
      handSortIndexChange() {
      },
      saveOrUpdate() {
        this.changeLoading = true
        const promise = this.position.positionId ? positionAPI.update(this.position) : positionAPI.save(this.position)
        promise.then(res => {
          this.changeLoading = false
          this.$emit('update',this.position.positionId)
          this.showChild = false
          this.resetForm('form')
        }).catch(err => {
          this.changeLoading = false
          console.log(err)
        })

      },
      getInfo(id) {
        positionAPI.getById(id).then(res => {
          this.position = res.result
        }).catch(err => {
          console.log(err)
        })
      },
      open(id) {
        // let-fix 应该有获取职位最大序号的api
        this.showChild = true
        this.position.positionId = id
        if (id) {
          this.title = '编辑职位'
          this.getInfo(id)
        } else {
          this.title = '新增职位'
        }
      },
      resetForm(form){
        this.$refs[form].resetFields()
      },
      submitForm(form){
        this.$refs[form].validate(valid => {
          if (valid && !this.changeLoading) {
            this.saveOrUpdate()
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">

</style>

